package video.background.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import video.background.Controller.View.FileSizeView;
import video.background.Controller.View.FileView;
import video.background.Dao.FileTableDao;
import video.background.Dao.TeamDao;
import video.background.Dao.UserDao;
import video.background.Dao.UserGroupDao;
import video.background.Model.FileTable;
import video.background.Model.Team;
import video.background.Model.UserGroup;
import video.background.Model.Users;
import video.background.Utils.JwtUtils;
import video.background.Utils.Result;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: video-backgroud
 * @description:
 * @author: hlt
 * @create: 2023-08-15 14:18
 **/
@RestController
@RequestMapping("/fileTable")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "团队文件相关接口")
public class FileTableController {

    @Autowired
    private UserGroupDao userGroupDao;

    @Autowired
    private FileTableDao fileTableDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private TeamDao teamDao;

    @GetMapping("/getFileListInTeam")
    @ApiOperation("获取团队文件列表")
    public Result getFileListInTeam(@RequestHeader String Authorization,
                                    @RequestParam Long groupId,
                                    @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                    @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize){
        Long userId = JwtUtils.decoderToken(Authorization);
        LambdaQueryWrapper<UserGroup> uglqw = new LambdaQueryWrapper<>();
        uglqw.eq(UserGroup::getUserId,userId).eq(UserGroup::getGroupId,groupId);
        UserGroup userGroup = userGroupDao.selectOne(uglqw);
        if(userGroup == null) return Result.Error("团队信息未找到！",null);
//        if(userGroup.getUserRole() == '2') return Result.Error("权限不足！", null);
        LambdaQueryWrapper<FileTable> ftlqw = new LambdaQueryWrapper<>();
        ftlqw.eq(FileTable::getGroupId,groupId);
        List<FileView> fileViews = fileTableDao.selectList(ftlqw).stream().map(fileTable -> {
            FileView fileView = new FileView();
            BeanUtils.copyProperties(fileTable, fileView);
            fileView.setFileSize(fileTable.getFileSize());
            // 上传文件的用户可能已经注销
            Users users = userDao.selectById(fileTable.getUserId());
            fileView.setUsername(users == null ? "用户已注销" : users.getUserName());
            return fileView;
        }).collect(Collectors.toList());
        int count = fileViews.size();
        Page<FileView> fileViewPage = new Page<>(currentPage, pageSize);
        fileViewPage.setTotal(count);
        fileViewPage.setRecords(fileViews);
        fileViewPage.setPages(count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
        return Result.Success("请求成功！", fileViewPage);
    }

    @GetMapping("/getTeamFileSize")
    @ApiOperation("获取团队当前储存的文件大小和最大文件容量")
    public Result getTeamFileSize(@RequestParam Long groupId){
        if(groupId == 0) return Result.Error("数据不全！",null);
        Team team = teamDao.selectById(groupId);
        if(team == null) return Result.Error("未查询到团队信息",null);
        FileSizeView fileSizeView = new FileSizeView(groupId, team.getCurrentFileSize(),
                (double) team.getFileCapacity());
        return Result.Success("请求成功！", fileSizeView);
    }
}
